package com.service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.DepDAO;
import com.pojo.Dep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@SuppressWarnings("ALL")
@Service
public class DepService  extends ServiceImpl<DepDAO, Dep> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private  DepDAO depDAO;

    /**
     * 先查缓存，缓存里面有，则返回
     * 如果缓存不存在，则查数据库，再存缓存
     * @return
     */
    @Cacheable(value = "dep",key = "'findAll'")
    public List findAll()
    {
        List list =depDAO.selectList(null);
        return  list;
    }

    /**
     * 删除缓存
     * @param id
     * @return
     */
    @CacheEvict(value = "dep",key = "#id",allEntries = true,beforeInvocation = true)
    public int delById(int id)
    {
        return depDAO.deleteById(id);
    }

    @Cacheable(value = "dep",key = "#id")
    public Dep findById(int id)
    {
        return depDAO.selectById(id);
    }

    @CacheEvict(value = "dep",key = "#id",allEntries = true,beforeInvocation = true)
    @CachePut(value = "dep",key = "#dep.depId")
    public int upudateDep(Dep dep)
    {
        return depDAO.updateById(dep);
    }

}
